package com.ruoyi.event.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.event.domain.EventPos;
import com.ruoyi.event.domain.bo.EventPosBo;
import com.ruoyi.event.domain.vo.EventPosVo;
import com.ruoyi.event.mapper.EventPosMapper;
import com.ruoyi.event.service.IEventPosService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户定位Service业务层处理
 *
 * @author strongme
 * @date 2023-04-05
 */
@RequiredArgsConstructor
@Service
public class EventPosServiceImpl implements IEventPosService {

    private final EventPosMapper baseMapper;

    /**
     * 查询用户定位
     */
    @Override
    public EventPosVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户定位列表
     */
    @Override
    public TableDataInfo<EventPosVo> queryPageList(EventPosBo bo, PageQuery pageQuery) {
        QueryWrapper<EventPos> lqw = buildQueryWrapper(bo);
        Page<EventPosVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户定位列表
     */
    @Override
    public List<EventPosVo> queryList(EventPosBo bo) {
        QueryWrapper<EventPos> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<EventPos> buildQueryWrapper(EventPosBo bo) {
        QueryWrapper<EventPos> lqw = Wrappers.query();
        if (CollUtil.isNotEmpty(bo.getUserIdList())) {
            lqw.select("id", "user_id", "lng", "lat", "address", "max(create_time) as create_time");
            lqw.groupBy("user_id");
        }else {
            if (bo.getUserId() != null) {
                lqw.eq("user_id", bo.getUserId());
            }else {
                lqw.select("id", "user_id", "lng", "lat", "address", "max(create_time) as create_time");
                lqw.groupBy("user_id");
            }
        }
        return lqw;
    }



    /**
     * 新增用户定位
     */
    @Override
    public Boolean insertByBo(EventPosBo bo) {
        EventPos add = BeanUtil.toBean(bo, EventPos.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户定位
     */
    @Override
    public Boolean updateByBo(EventPosBo bo) {
        EventPos update = BeanUtil.toBean(bo, EventPos.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EventPos entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户定位
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
